package org.xlx.xlx_oa.role_user.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xlx.xlx_oa.common.BusinessException;
import org.xlx.xlx_oa.common.ResponseBean;
import org.xlx.xlx_oa.role_user.api.AdmDepartmentApi;
import org.xlx.xlx_oa.role_user.domain.vo.AdmDepartmentVo;
import org.xlx.xlx_oa.role_user.service.AdmDepartmentService;

@RestController
@RequestMapping("/v1/adm-department")
public class AdmDepartmentController implements AdmDepartmentApi {
    @Autowired
    private AdmDepartmentService departmentService;

    @PostMapping("/save")
    @Override
    public ResponseBean saveEntity(@RequestBody AdmDepartmentVo departmentVo) {
        return ResponseBean.successFactory(departmentService.saveDepartment(departmentVo));
    }

    @DeleteMapping("/remove/{id}")
    @Override
    public ResponseBean removeEntity(@PathVariable(value = "id") Long id) {
        try {
            return ResponseBean.successFactory(departmentService.removeDepartment(id));
        } catch (BusinessException e) {
            return ResponseBean.failureFactory(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return ResponseBean.failureFactory(e.getMessage());
        }
    }

    @PostMapping("/list")
    @Override
    public ResponseBean getList(@RequestBody AdmDepartmentVo departmentVo) {
        return ResponseBean.successFactory(departmentService.getList(departmentVo));
    }

    @PostMapping("/count")
    @Override
    public ResponseBean getCount(@RequestBody AdmDepartmentVo departmentVo) {
        return ResponseBean.successFactory(departmentService.getCount(departmentVo));
    }


    @GetMapping("/detail/{id}")
    @Override
    public ResponseBean getDetail(@PathVariable(value = "id") Long id) {
        return ResponseBean.successFactory(departmentService.getDetail(id));
    }
}

